home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1995 August: Tool Chest / Dev.CD Aug 95 TC / Dev.CD Aug 95 TC.toast / Tool Chest / Development Tools & Languages / Dylan Related / Marlais / Marlais 0.5.9-portable sources / array.c < prev    next >
Encoding:
C/C++ Source or Header  |  1995-03-15  |  6.2 KB  |  247 lines  |  [TEXT/ttxt]

  1. /*
  2.  
  3.    array.c
  4.  
  5.    This software is free software; you can redistribute it and/or
  6.    modify it under the terms of the GNU Library General Public
  7.    License as published by the Free Software Foundation; either
  8.    version 2 of the License, or (at your option) any later version.
  9.  
  10.    This software is distributed in the hope that it will be useful,
  11.    but WITHOUT ANY WARRANTY; without even the implied warranty of
  12.    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  13.    Library General Public License for more details.
  14.  
  15.    You should have received a copy of the GNU Library General Public
  16.    License along with this software; if not, write to the Free
  17.    Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  18.  
  19.    Original copyright notice follows:
  20.  
  21.    Copyright, 1993, Brent Benson.  All Rights Reserved.
  22.    0.4 & 0.5 Revisions Copyright 1994, Joseph N. Wilson.  All Rights Reserved.
  23.  
  24.    Permission to use, copy, and modify this software and its
  25.    documentation is hereby granted only under the following terms and
  26.    conditions.  Both the above copyright notice and this permission
  27.    notice must appear in all copies of the software, derivative works
  28.    or modified version, and both notices must appear in supporting
  29.    documentation.  Users of this software agree to the terms and
  30.    conditions set forth in this notice.
  31.  
  32.  */
  33.  
  34. #include "array.h"
  35.  
  36. #include "alloc.h"
  37. #include "error.h"
  38. #include "number.h"
  39. #include "prim.h"
  40. #include "symbol.h"
  41.  
  42. static Object row_major_index (Object arr, Object indices);
  43. static Object array_element (Object arr, Object indices, Object default_ob);
  44. static Object array_element_setter (Object arr, Object indices, Object new_val);
  45. static Object array_dimensions (Object arr);
  46. static Object array_initial_state (Object arr);
  47. static Object array_next_state (Object arr, Object state);
  48. static Object array_current_element (Object arr, Object state);
  49.  
  50. static struct primitive array_prims[] =
  51. {
  52.     {"%array-element", prim_3, array_element},
  53.     {"%array-element-setter", prim_3, array_element_setter},
  54.     {"%array-dimensions", prim_1, array_dimensions},
  55.     {"%array-initial-state", prim_1, array_initial_state},
  56.     {"%array-next-state", prim_2, array_next_state},
  57.     {"%array-current-element", prim_2, array_current_element},
  58.     {"%array-row-major-index", prim_2, row_major_index},
  59. };
  60.  
  61. void
  62. init_array_prims (void)
  63. {
  64.     int num;
  65.  
  66.     num = sizeof (array_prims) / sizeof (struct primitive);
  67.  
  68.     init_prims (num, array_prims);
  69. }
  70.  
  71. Object
  72. make_array (Object dims, Object fill)
  73. {
  74.     Object obj, dl, val;
  75.     unsigned int size, i;
  76.  
  77.     obj = allocate_object (sizeof (struct array));
  78.  
  79.     ARRTYPE (obj) = Array;
  80.     ARRDIMS (obj) = dims;
  81.     dl = dims;
  82.     size = 1;
  83.     while (!NULLP (dl)) {
  84.     val = CAR (dl);
  85.     if (!INTEGERP (val)) {
  86.         error ("make: array dimensions must be integers", dims, NULL);
  87.     }
  88.     size *= INTVAL (val);
  89.     dl = CDR (dl);
  90.     }
  91.     ARRELS (obj) = (Object *) checking_malloc (sizeof (Object) * size);
  92.  
  93.     for (i = 0; i < size; ++i) {
  94.     ARRELS (obj)[i] = fill;
  95.     }
  96.     return (obj);
  97. }
  98.  
  99. Object
  100. make_array_driver (Object args)
  101. {
  102.     int size, i;
  103.     Object dim_obj, fill_obj, res;
  104.  
  105.     size = 0;
  106.     dim_obj = NULL;
  107.     fill_obj = NULL;
  108.  
  109.     while (!NULLP (args)) {
  110.     if (FIRST (args) == dim_keyword) {
  111.         dim_obj = SECOND (args);
  112.     } else if (FIRST (args) == fill_keyword) {
  113.         fill_obj = SECOND (args);
  114.     } else {
  115.         error ("make: unsupported keyword for <array> class", FIRST (args), NULL);
  116.     }
  117.     args = CDR (CDR (args));
  118.     }
  119.     if (dim_obj) {
  120.     if (!PAIRP (dim_obj)) {
  121.         error ("make: value of dimensions: "
  122.            "argument must be a list of integers", dim_obj, NULL);
  123.     }
  124.     } else {
  125.     error ("make: dimensions: must be specified for <array>", args, NULL);
  126.     }
  127.     if (!fill_obj) {
  128.     fill_obj = false_object;
  129.     }
  130.     /* actually fabricate the array */
  131.     res = make_array (dim_obj, fill_obj);
  132.     return (res);
  133. }
  134.  
  135. /* static functions */
  136.  
  137. static int
  138. index (Object arr, Object indices, Object default_ob)
  139. {
  140.     Object dims, inds, ind, dim;
  141.     unsigned int offset, dim_val, ind_val, last_dim_val;
  142.  
  143.     dims = ARRDIMS (arr);
  144.     inds = indices;
  145.     offset = 0;
  146.     last_dim_val = 1;
  147.  
  148.     while (!NULLP (dims) && !NULLP (inds)) {
  149.     if (NULLP (dims)) {
  150.         error ("element: too many indices for array", arr, indices, NULL);
  151.     }
  152.     if (NULLP (inds)) {
  153.         error ("element: not enough indices given", arr, indices, NULL);
  154.     }
  155.     dim = CAR (dims);
  156.     ind = CAR (inds);
  157.     if (!INTEGERP (ind)) {
  158.         error ("element: array indices must be integers", ind, NULL);
  159.     }
  160.     dim_val = INTVAL (dim);
  161.     ind_val = INTVAL (ind);
  162.     if ((ind_val < 0) || (ind_val >= dim_val)) {
  163.         if (default_ob == default_object) {
  164.         error ("element: array indices out of range", indices,
  165.                ARRDIMS (arr), NULL);
  166.         } else {
  167.         return -1;
  168.         }
  169.     }
  170.     offset += (ind_val * last_dim_val);
  171.     last_dim_val = dim_val;
  172.     dims = CDR (dims);
  173.     inds = CDR (inds);
  174.     }
  175.     if (!NULLP (dims)) {
  176.     error ("element: not enough indices for array", arr, indices, NULL);
  177.     }
  178.     if (!NULLP (inds)) {
  179.     error ("element: too many indices given", arr, indices, NULL);
  180.     }
  181.     return offset;
  182. }
  183.  
  184. static Object
  185. row_major_index (Object arr, Object indices)
  186. {
  187.     return make_integer (index (arr, indices, default_object));
  188. }
  189.  
  190.  
  191. /*
  192.  * Note that the key of an array collection is the list of indices.
  193.  */
  194.  
  195. static Object
  196. array_element (Object arr, Object indices, Object default_ob)
  197. {
  198.     return (ARRELS (arr)[index (arr, indices, default_ob)]);
  199. }
  200.  
  201. static Object
  202. array_element_setter (Object arr, Object indices, Object new_val)
  203. {
  204.     ARRELS (arr)[index (arr, indices, default_object)] = new_val;
  205.     return (unspecified_object);
  206. }
  207.  
  208. static Object
  209. array_dimensions (Object arr)
  210. {
  211.     return (ARRDIMS (arr));
  212. }
  213.  
  214. static Object
  215. array_initial_state (Object arr)
  216. {
  217.     return (make_integer (0));
  218. }
  219.  
  220. static Object
  221. array_next_state (Object arr, Object state)
  222. {
  223.     Object dims, dim;
  224.     unsigned int total_size, state_val;
  225.  
  226.     total_size = 1;
  227.     dims = ARRDIMS (arr);
  228.     while (!NULLP (dims)) {
  229.     dim = CAR (dims);
  230.     total_size *= INTVAL (dim);
  231.     dims = CDR (dims);
  232.     }
  233.     state_val = INTVAL (state);
  234.     state_val++;
  235.     if (state_val >= total_size) {
  236.     return (false_object);
  237.     } else {
  238.     return (make_integer (state_val));
  239.     }
  240. }
  241.  
  242. static Object
  243. array_current_element (Object arr, Object state)
  244. {
  245.     return (ARRELS (arr)[INTVAL (state)]);
  246. }
  247.